package com.htthorizon.powersupply.controller;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.htthorizon.powersupply.domain.RealTimePowerOutage;
import com.htthorizon.powersupply.domain.ReportTgLowControl;
import com.htthorizon.powersupply.domain.ReportTgLowVolt;
import com.htthorizon.powersupply.dto.TgLowVoltItem;
import com.htthorizon.powersupply.search.ReportLoadMonitorSearch;
import com.htthorizon.powersupply.service.RealTimePowerOutageService;
import com.htthorizon.powersupply.service.ReportLoadMonitorService;
import com.htthorizon.powersupply.service.ReportTgLowControlService;
import com.htthorizon.powersupply.service.ReportTgLowVoltService;
import com.htthorizon.powersupply.service.TgVoltMonitorService;
import com.htthorizon.powersupply.service.UserOutageMonitorService;
import com.htthorizon.powersupply.utils.R;
import com.htthorizon.powersupply.vo.ReportTgLoadMonitorVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/report")
@Slf4j
public class ReportController {

    @Autowired
    private ReportLoadMonitorService reportLoadMonitorService;

    @Autowired
    private TgVoltMonitorService tgVoltMonitorService;

    @Autowired
    private ReportTgLowControlService reportTgLowControlService;

    @Autowired
    private ReportTgLowVoltService reportTgLowVoltService;

    @Autowired
    private UserOutageMonitorService userOutageMonitorService;

    @Autowired
    private RealTimePowerOutageService realTimePowerOutageService;

    @GetMapping("/load")
    public R<List<ReportTgLoadMonitorVo>> load(ReportLoadMonitorSearch reportLoadMonitorSearch) {
        if (reportLoadMonitorSearch.getZbflag()) {
            reportLoadMonitorSearch.setTgType("专变");
        } else {
            reportLoadMonitorSearch.setTgType("公变");
        }
        List<ReportTgLoadMonitorVo> list = reportLoadMonitorService.load(reportLoadMonitorSearch);
        if (list.size() > 0) {
            for (ReportTgLoadMonitorVo item : list) {
                String newApperTime = item.getNewApperTime();
                DateTime parse = DateUtil.parse(newApperTime, "yyyy-MM-dd HH:mm");
                String updateTime = item.getUpdateTime();
                DateTime parse1 = DateUtil.parse(updateTime, "yyyy-MM-dd HH:mm:ss");
                boolean before = parse.before(parse1);
                item.setIs(before ? "true" : "false");

            }
        }
        return R.ok(list);
    }

    @PostMapping("/loadEdit")
    public R<String> loadEdit(@RequestBody ReportTgLoadMonitorVo reportTgLoadMonitorVo) {
        String updateTime = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss");
        reportTgLoadMonitorVo.setUpdateTime(updateTime);
        reportLoadMonitorService.loadEdit(reportTgLoadMonitorVo);
        return R.ok("loadEdit");
    }

    // 低电压
    @GetMapping("/page2List")
    public R<List<ReportTgLowVolt>> tgVolt(
            @RequestParam(value = "dataDt", required = false) String dataDt,
            @RequestParam(value = "lineNm", required = false) String lineNm,
            @RequestParam(value = "stdCountyOrgNm", required = false) String stdCountyOrgNm,
            @RequestParam(value = "stdOrgNm", required = false) String stdOrgNm
    ) {
//        List<TgLowVoltItem> list = tgVoltMonitorService.getStatistics("all", null, null);
//        for (TgLowVoltItem item : list) {
//
//        }
        LambdaQueryWrapper<ReportTgLowVolt> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.isNotEmpty(lineNm), ReportTgLowVolt::getLineNm, lineNm);
        wrapper.like(StringUtils.isNotEmpty(stdCountyOrgNm), ReportTgLowVolt::getStdCountyOrgNm, stdCountyOrgNm);
        wrapper.like(StringUtils.isNotEmpty(stdOrgNm), ReportTgLowVolt::getStdOrgNm, stdOrgNm);
        wrapper.orderByDesc(ReportTgLowVolt::getDataDt);
        String dateDt = DatePattern.PURE_DATE_FORMAT.format(new Date());

        List<TgLowVoltItem> tgLowVoltItems = tgVoltMonitorService.getStatistics("today", null, dateDt);
        log.info("TgLowVoltItems:{}", tgLowVoltItems.size());

        if (tgLowVoltItems.size() > 0) {
            for (TgLowVoltItem item : tgLowVoltItems) {
                ReportTgLowVolt reportTgLowVolt = new ReportTgLowVolt();
                BeanUtils.copyProperties(item, reportTgLowVolt);
                reportTgLowVolt.setCustSum(item.getUserCount());
                reportTgLowVolt.setLoadDate(item.getTm());
                reportTgLowVolt.setTgSum(item.getTgCount());
                reportTgLowVolt.setDataDt(dateDt);
                reportTgLowVoltService.insertReport(reportTgLowVolt);
            }
        }
        List<ReportTgLowVolt> list = reportTgLowVoltService.list(wrapper);
        return R.ok(list);
    }

    @PostMapping("/loadEdit2 ")
    public R<String> tgVoltEdit(@RequestBody ReportTgLowVolt reportTgLowVolt) {
        reportTgLowVoltService.updateById(reportTgLowVolt);
        return R.ok("tgVoltEdit");
    }

    // 停电监测
    @GetMapping("/page3List")
    public R<List<ReportTgLowControl>> tgStop(@RequestParam(value = "dataDt", required = false) String dataDt,
                                            @RequestParam(value = "lineNm", required = false) String lineNm,
                                            @RequestParam(value = "stdCountyOrgNm", required = false) String stdCountyOrgNm,
                                            @RequestParam(value = "stdOrgNm", required = false) String stdOrgNm
    ) {
        LambdaQueryWrapper<ReportTgLowControl> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotEmpty(dataDt),ReportTgLowControl::getDataDt, dataDt);
        wrapper.like(StringUtils.isNotEmpty(lineNm), ReportTgLowControl::getLineNm, lineNm);
        wrapper.like(StringUtils.isNotEmpty(stdCountyOrgNm), ReportTgLowControl::getStdCountyOrgNm, stdCountyOrgNm);
        wrapper.like(StringUtils.isNotEmpty(stdOrgNm), ReportTgLowControl::getStdOrgNm, stdOrgNm);
        wrapper.orderByDesc(ReportTgLowControl::getDataDt);

        String format = DateUtil.format(DateUtil.date(), "yyyy-MM-dd HH:mm:ss");
        new Thread(() -> {
            List<RealTimePowerOutage> realTimePowerOutages= realTimePowerOutageService.list();
            log.info("realTimePowerOutages:{}", realTimePowerOutages.size());
            if (realTimePowerOutages.size() > 0) {
                for (RealTimePowerOutage item : realTimePowerOutages) {
                    try {
                        ReportTgLowControl reportTgLowControl = new ReportTgLowControl();
                        reportTgLowControl.setDataDt(item.getDataDt());
                        reportTgLowControl.setLineNm(item.getLineNm());
                        reportTgLowControl.setStdCountyOrgNm(item.getContyOrgNm());
                        reportTgLowControl.setStdOrgNm(item.getTgOrgNm());
                        reportTgLowControl.setTgNm(item.getTgNm());
                        reportTgLowControl.setStartTm(DateUtil.format(item.getEd(), "yyyy-MM-dd HH:mm:ss"));
                        reportTgLowControl.setUpdateDate(format);
                        reportTgLowControlService.insertReport(reportTgLowControl);
                    } catch (Exception e) {
                        log.error("page3List error:{}", e.getMessage());
                    }
                }
            }
        }).start();



        List<ReportTgLowControl> list = reportTgLowControlService.list(wrapper);
        return R.ok(list);
    }
//    @GetMapping("/tgStopEdit")
//    public R<String> tgStopEdit(@RequestBody){
//        reportTgLowVoltService.updateById()
//    }

    @PostMapping("/loadEdit3")
    public R<String> loadedit3(@RequestBody ReportTgLowControl reportTgLowControl) {
        reportTgLowControlService.updateById(reportTgLowControl);
        return R.ok("loadedit3");
    }

}
